<template>
  <div class="queryStudentInfo">
    <div>
      <!--搜索表单和添加表单-->
      <div style="display: flex;margin-bottom: 10px">
        <el-select v-model="params.cname" placeholder="请选择课程">
          <el-option
              v-for="item in courses"
              :label="item.cname"
              :value="item.cname">
          </el-option>
        </el-select>
        <el-button class="el-icon-search" type="primary" style="margin-left: 5px" @click="search">查询</el-button>
        <el-button class="el-icon-refresh" type="primary" style="margin-left: 5px" @click="reset">重置</el-button>
      </div>
    </div>

    <!--主体-->
    <el-table :data="tableData" :cell-style="{'text-align':'center'}" :header-cell-style="{'text-align':'center'}"
              stripe>
      <el-table-column prop="sno" label="学号"></el-table-column>
      <el-table-column prop="sname" label="姓名"></el-table-column>
      <el-table-column prop="ssex" label="性别"></el-table-column>
      <el-table-column prop="sage" label="年龄"></el-table-column>
      <el-table-column prop="sdept" label="系别"></el-table-column>
      <el-table-column prop="grade" label="成绩">
        <template v-slot="scope">
          <el-input v-model="scope.row.grade" min="0" max="100" style="width: 60px"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button type="primary" :disabled="status === '0'" style="margin-left: 5px"
                     @click="confirm(scope.row.sno,scope.row.grade)">确认打分
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <div style="margin-top: 10px">
      <el-pagination
          background
          :current-page="params.pageNum"
          :page-size="params.pageSize"
          @current-change="handleCurrentChange"
          layout="prev, pager, next"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import request2 from "@/utils/request2";

export default {
  name: "GiveGrade",
  data() {
    return {
      tno: JSON.parse(Cookies.get('teacher')).username,
      total: 0,
      status: '',
      courses: [],
      tableData: [],
      params: {
        pageNum: 1,
        pageSize: 10,
        cname: '',
        tno: ''
      },
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      request2.get('/teacherOfTeacher/getCourse/' + this.tno).then(res => {
        this.courses = res.data
      })
      request2.get('/timeSet/getTeacher').then(res => {
        this.status = res.data.status
      })
    },
    handleCurrentChange(pageNum) {
      this.params.pageNum = pageNum
      this.load()
    },
    search() {
      this.params.tno = this.tno
      request2.get('/teacherOfTeacher/page', {params: this.params}).then(res => {
        this.tableData = res.data.list
        this.total = res.data.total
      })
    },
    reset() {
      this.params = {
        pageNum: 1,
        pageSize: 10,
        cname: '',
        tno: ''
      }
      this.search()
    },
    confirm(sno, grade) {
      if (parseInt(grade) < 0 || parseInt(grade) > 100) {
        this.$notify.error('分数只能在0-100范围内')
      } else {
        request2.put('/teacherOfTeacher/giveGrade/' + sno + '/' + this.params.cname + '/' + this.tno + '/' + grade).then(res => {
          if (res.code === '200') {
            this.$notify.success('打分成功')
            this.load()
          } else {
            this.$notify.success('打分失败')
          }
        })
      }
    }
  }
}
</script>
